﻿//------------------------------------------------------------------------------
// <copyright file="WebDataService.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Data.Services;
using System.Data.Services.Common;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Web;
using System.Data.Services.Providers;
using System.Data.Services.Client;


namespace CalligraphyDataService
{
    public class WcfDataService : DataService<CalligraphyEntities>, IServiceProvider
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
            // Examples:
            // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);
            // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
            config.SetEntitySetAccessRule("T_Author", EntitySetRights.All);
            config.SetEntitySetAccessRule("T_Dynast", EntitySetRights.AllRead | EntitySetRights.AllWrite);
            config.SetEntitySetAccessRule("T_Photo", EntitySetRights.ReadMultiple | EntitySetRights.ReadSingle | EntitySetRights.AllWrite);
            config.SetEntitySetAccessRule("T_Rubbing", EntitySetRights.All);
            config.SetEntitySetAccessRule("T_StelePaper", EntitySetRights.All);
            config.SetEntitySetAccessRule("T_RubbingPhoto", EntitySetRights.All);            
            config.SetEntitySetAccessRule("T_PaperPhoto", EntitySetRights.All);
            config.SetEntitySetAccessRule("T_Style", EntitySetRights.All);
            config.SetEntitySetAccessRule("T_WordPhoto", EntitySetRights.All);
            config.SetServiceOperationAccessRule("SearchRubbingPhoto", ServiceOperationRights.AllRead);
            config.SetServiceOperationAccessRule("SearchPaperPhoto", ServiceOperationRights.AllRead);
            config.SetServiceOperationAccessRule("SearchAllAuthor", ServiceOperationRights.AllRead);
            config.SetServiceOperationAccessRule("SearchAllStyle", ServiceOperationRights.AllRead);

            config.SetEntitySetAccessRule("T_Video", EntitySetRights.All);


            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
        }

        public object GetService(Type serviceType)
        {
            if (serviceType == typeof(IDataServiceStreamProvider))
            {
                return new PhotoStreamProvider(this.CurrentDataSource);
            }
            return null;
        }
        [WebGet]
        public IQueryable<T_RubbingPhoto> SearchRubbingPhoto(String queryText)
        {
            var context = this.CurrentDataSource;
            //DataLoadOptions dlo = new DataLoadOptions();
            //dlo.LoadWith<T_RubbingPhoto>(c => c.T_Rubbing);
            //context.LoadOptions = dlo;

            
            IQueryable<T_RubbingPhoto> searchInRubbing =
                //from rp in context.T_RubbingPhoto.Expand("T_Photo").Expand("T_Rubbing").Expand("T_Rubbing/T_StelePaper").Expand("T_Rubbing/T_StelePaper/T_Author").Where(p => p.Text.Contains(queryText) && (p.IsDeleted == null || p.IsDeleted == false));
                from rp in context.T_RubbingPhoto.Include("T_Photo").Include("T_Rubbing").Include("T_Rubbing/T_StelePaper").Include("T_Rubbing/T_StelePaper/T_Author").Include("T_Rubbing/T_StelePaper/T_Style").Include("T_Rubbing/T_StelePaper/T_Dynast")
                join photo in context.T_Photo on rp.PhotoID equals photo.ID
                join rubbing in context.T_Rubbing on rp.RubbingID equals rubbing.ID
                join stelePaper in context.T_StelePaper on rubbing.StelePaperID equals stelePaper.ID
                join author in context.T_Author on stelePaper.AuthorID equals author.ID
                where rp.Text.Contains(queryText) && (rp.IsDeleted == null || rp.IsDeleted == false) && (rp.T_Rubbing.IsDeleted == null || rp.T_Rubbing.IsDeleted == false) && (rp.T_Rubbing.T_StelePaper.IsDeleted == null || rp.T_Rubbing.T_StelePaper.IsDeleted == false) && (rp.T_Rubbing.T_StelePaper.T_Author.IsDeleted == null || rp.T_Rubbing.T_StelePaper.T_Author.IsDeleted == false)
                orderby rp.T_Rubbing.T_StelePaper.T_Dynast.ID
                select rp;

            return searchInRubbing;
        }
        [WebGet]
        public IQueryable<T_PaperPhoto> SearchPaperPhoto(string queryText)
        {
            //((DataServiceQuery<T_PaperPhoto>)DataServiceContextHelper.DataContext.T_PaperPhoto.Expand("T_Photo").Expand("T_StelePaper").Expand("T_StelePaper/T_Author").Where(p => p.Text.Contains(queryText) && (p.IsDeleted == null || p.IsDeleted == false))).ExecuteAsync<T_PaperPhoto>();
            var context = this.CurrentDataSource;
            IQueryable<T_PaperPhoto> searchInPaper =
                //from pp in context.T_PaperPhoto.Include("T_Photo").Include("T_StelePaper").Include("T_StelePaper/T_Author").Include("T_StelePaper/T_Style").Include("T_StelePaper/T_Dynast")
                from pp in context.T_PaperPhoto.Include("T_Photo").Include("T_StelePaper").Include("T_Author").Include("T_Style").Include("T_Dynast")
                join photo in context.T_Photo on pp.PhotoID equals photo.ID
                join stelePaper in context.T_StelePaper on pp.StelePaperID equals stelePaper.ID
                join author in context.T_Author on stelePaper.AuthorID equals author.ID
                where pp.Text.Contains(queryText) && (pp.IsDeleted == null || pp.IsDeleted == false) && (pp.T_StelePaper.IsDeleted == null || pp.T_StelePaper.IsDeleted == false) && (pp.T_StelePaper.T_Author.IsDeleted == null||pp.T_StelePaper.T_Author.IsDeleted ==false)
                orderby pp.T_StelePaper.T_Dynast.ID
                select pp;
            return searchInPaper;
        }
        [WebGet]
        public IQueryable<T_Author> SearchAllAuthor()
        {
            var context = this.CurrentDataSource;
            IQueryable<T_Author> authors =
                from author in context.T_Author
                where author.T_StelePaper.Count()>0 && author.IsDeleted==null || author.IsDeleted==false
                orderby author.T_Dynast.ID
                select author;
            return authors;
        }
        [WebGet]
        public IQueryable<T_Style> SearchAllStyle()
        {
            var context = this.CurrentDataSource;
            IQueryable<T_Style> styles =
                from style in context.T_Style
                select style;
            return styles;
        }
    }
}
